number=int(input('введите четырехзначное число'))
p = 0
l = 0
o = 0
k = 0
m = 0
j = 0
u = 0
h = 0
y = 0
ab1 = 0
if 999 < number < 10000:
    a1 = number // 1000
    b1 = number // 100 % 10
    c1 = number % 100 // 10
    d1 = number % 10
    ab = number // 100
    bc = number // 10 % 100
    cd = number % 100
    abc = number // 10
    bcd = number % 1000
    for i in range(2, a1 // 2+1):
        if (a1 % i == 0):
            p += 1
    if p <= 0:
        a2 = a1
    for i in range(2, b1 // 2+1):
        if (b1 % i == 0):
            l += 1
    if l <= 0:
        b2 = b1
    for i in range(2, c1 // 2+1):
        if (c1 % i == 0):
            o += 1
    if o <= 0:
        c2 = c1
    for i in range(2, d1 // 2+1):
        if (d1 % i == 0):
            k = k+1
    if k <= 0:
        d2 = d1
    for i in range(2, ab // 2+1):
        if (ab % i == 0):
            m += 1
    if m <= 0:
        ab1 = ab
    for i in range(2, bc // 2+1):
        if (bc % i == 0):
            j += 1
    if j <= 0:
        bc1 = bc
    for i in range(2, cd // 2+1):
        if (cd % i == 0):
            u += 1
    if u <= 0:
        cd1 = cd
    for i in range(2, abc // 2+1):
        if (abc % i == 0):
            h += 1
    if h <= 0:
        abc1 = abc
    for i in range(2, bcd // 2+1):
        if (bcd % i == 0):
            y += 1
    if y <= 0:
        bcd1 = bcd
    elif a2 > b2 and a2 > c2 and a2 > d2 and a2 > ab1 and a2 > bc1 and a2 > cd1 and a2 > abc1 and a2 > bcd1:
        print(a2)
    elif b2 > a2 and b2 > c2 and b2 > d2 and b2 > ab1 and b2 > bc1 and b2 > cd1 and b2 > abc1 and b2 > bcd1:
        print(b2)
    elif c2 > b2 and c2 > a2 and c2 > d2 and c2 > ab1 and c2 > bc1 and c2 > cd1 and c2 > abc1 and c2 > bcd1:
        print(c2)
    elif d2 > b2 and d2 > c2 and d2 > a2 and d2 > ab1 and d2 > bc1 and d2 > cd1 and d2 > abc1 and d2 > bcd1:
        print(d2)
    elif ab1 > b2 and ab1 > c2 and ab1 > d2 and ab1 > a2 and ab1 > bc1 and ab1 > cd1 and ab1 > abc1 and ab1 > bcd1:
        print(ab1)
    elif bc1 > b2 and bc1 > c2 and bc1 > d2 and bc1 > ab1 and bc1 > a2 and bc1 > cd1 and bc1 > abc1 and bc1 > bcd1:
        print(bc1)
    elif cd1 > b2 and cd1 > c2 and cd1 > d2 and cd1 > ab1 and cd1 > bc1 and cd1 > a2 and cd1 > abc1 and cd1 > bcd1:
        print(cd1)
    elif abc1 > b2 and abc1 > c2 and abc1 > d2 and abc1 > ab1 and abc1 > bc1 and abc1 > cd1 and abc1 > a2 and abc1 > bcd1:
        print(abc1)
    elif bcd1 > b2 and bcd1 > c2 and bcd1 > d2 and bcd1 > ab1 and bcd1 > bc1 and bcd1 > cd1 and bcd1 > abc1 and bcd1 > a2:
        print(bcd1)
else:
    print('0')
